{
  File   : S2DType.pas (This unit is a part of S2DE)
  S2DE   : Simple 2D Engine (software render)
  Copyright (c) 2008, Kagamma
  License: GPLv3

-----------------------------------------------------------
  Chua cac record & constants co ban cua S2DE.
}

unit S2DTypes;

{$MODE OBJFPC}

interface

uses
  Windows;

const
  Version         = '0.0.1a i386-win32'; // S2DE version
  S2DClass: PChar = 'S2DE';
  DEG2RAD         = 0.0174533;

// record chua cac thong tin chinh cua S2DE
type
  TS2DInfo = record
    Width,
    Height   : LongInt;
    WndHandle: hWnd;
    DC       : hDC;
    GLRC     : HGLRC;
    Device   : TDevMode;
    DeviceOK : Boolean;
    AMessage : TMsg;
    Title    : PChar;
    Renderer : Byte;
    FPSStart,
    FPS,
    FPSCount,
    FPSLast,
    FPSElapse,
    TickCount,
    TickStart,
    TotalAllocMemory,
    MaxAllocMemory: DWord;
    Maximize,
    Running  : Boolean;
  end;

  // Buffer - chua cac thong tin ve vung nho lam viec
  PBuffer = ^TBuffer;
  TBuffer = record
    Address,
    Width,
    Height,
    Size,
    Area,
    MiddleX,
    MiddleY,
    MidXM1,
    MidYM1,
    ScanX,
    ScanY,
    ID    : DWord;
    Path  : String;
    HaveID: Boolean;
  end;

  // Cau truc cua 1 gia tri mau 24bit
  TRGB = record
    R, G, B: Byte;
  end;

  // Vector & Matrix
  TVector2f = record
    X, Y: Single;
  end;

  TVector3f = record
    X, Y, Z: Single;
  end;
  TVertex = TVector3f;

  PMatrix3   = ^TMatrix3;
  PMatrix4   = ^TMatrix4;
  PMatrix3x3 = ^TMatrix3x3;
  PMatrix4x4 = ^TMatrix4x4;
  TMatrix3   = array[0..3] of Single;
  TMatrix4   = array[0..3] of Single;
  TMatrix3x3 = array[0..2, 0..2] of Single;
  TMatrix4x4 = array[0..3, 0..3] of Single;

var
  S2DInfo   : TS2DInfo;
  IsS2DStart: Boolean;

  // sintab & costab chua cac gia tri cua sin & cos
  SinTab,
  CosTab    : array[0..360] of Single;

  // keyboard mapping
  KeyMap    : array[0..256] of Boolean;
  // Mouse handling
  MouseX,
  MouseY    : Word;
  MouseMove,
  MouseM,
  MouseL,
  MouseR    : Boolean;
  FrameTime : Word;

function GetSin(Deg: Integer): Single;
function GetCos(Deg: Integer): Single;

implementation

function GetSin(Deg: Integer): Single;
begin
  if Deg>360 then
    Deg:= Deg mod 360
  else
  if Deg<0 then
    Deg:= (Deg mod 360)+360;
  Result:= SinTab[Deg];
end;

function GetCos(Deg: Integer): Single;
begin
  if Deg>360 then
    Deg:= Deg mod 360
  else
  if Deg<0 then
    Deg:= (Deg mod 360)+360;
  Result:= CosTab[Deg];
end;

var I  : Word;
    Val: Single;

begin
  // khoi tao cac gia tri cho 1 ung dung S2DE
  S2DInfo.Running:= FALSE;
  S2DInfo.TotalAllocMemory:= 0;
  S2DInfo.MaxAllocMemory:= 0;
  S2DInfo.DeviceOK:= FALSE;
  S2DInfo.FPS:= 60;
  S2DInfo.FPSStart:= GetTickCount;
  S2DInfo.FPSElapse:= 0;
  S2DInfo.FPSLast:= 0;
  FrameTime := 0;
  S2DInfo.FPSCount:= 0;
  for I:= 0 to 360 do
  begin
    SinTab[I]:= Sin(Val);
    CosTab[I]:= Cos(Val);
    Val:= Val + (Pi/180);
  end;
end.

